/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Visual;

import Logica.Localidad;
import Persistencia.exceptions.PreexistingEntityException;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JDesktopPane;
import javax.swing.JComboBox;

/**
 *
 * @author Alberto
 */
public class frmAltaTaller extends javax.swing.JInternalFrame{

    ControladoraVisual miVisual;
    JDesktopPane miEscritorio;
    DefaultComboBoxModel modeloLocalidades = new DefaultComboBoxModel();
    DefaultListModel modeloZonas = new DefaultListModel();
    
    /**
     * Creates new form AltaTaller
     */
    public frmAltaTaller(ControladoraVisual miVisual, JDesktopPane miEscritorio) {
        initComponents();
        this.miEscritorio = miEscritorio;
        this.miVisual = miVisual;
        this.cargarLocalidades();
        this.listZonasCobertura.setModel(modeloZonas);
        
    }
    
  
    public void cargarLocalidades(){
       
        List<Localidad> localidades = miVisual.dameLocalidades();
        for(Localidad miLocalidad:localidades){
            modeloLocalidades.addElement(miLocalidad);
        }
        this.cmbLocalidades.setModel(modeloLocalidades);
    }
    
    public List<Localidad> crearListaZonas(){
        int a = 0;  
        List<Localidad> zonas = new LinkedList();
        System.out.println(this.listZonasCobertura.getModel().getSize());
       while((this.listZonasCobertura.getModel().getSize())> a){
            System.out.println(this.listZonasCobertura.getModel().getSize());
            Localidad miZona = (Localidad) this.listZonasCobertura.getModel().getElementAt(a);
            zonas.add(miZona);
            a++;
        }
        return zonas;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnSalir = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtNroTaller = new javax.swing.JTextField();
        txtNombre = new javax.swing.JTextField();
        txtDireccion = new javax.swing.JTextField();
        txtTelefono = new javax.swing.JTextField();
        cmbLocalidades = new javax.swing.JComboBox();
        btnAltaLocalidad = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        listZonasCobertura = new javax.swing.JList();
        jLabel6 = new javax.swing.JLabel();
        btnAgregarZonaCobertura = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        btnAltaTaller = new javax.swing.JButton();

        setTitle("Alta Taller");

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        jLabel1.setText("Nro de Taller");

        jLabel2.setText("Nombre");

        jLabel3.setText("Direccion");

        jLabel4.setText("Telefono");

        jLabel5.setText("Localidad");

        cmbLocalidades.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbLocalidadesActionPerformed(evt);
            }
        });

        btnAltaLocalidad.setText("Nueva Localidad");
        btnAltaLocalidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaLocalidadActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(listZonasCobertura);

        jLabel6.setText("Zonas de Cobertura:");

        btnAgregarZonaCobertura.setText("Agregar Zona de Cobertura");
        btnAgregarZonaCobertura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarZonaCoberturaActionPerformed(evt);
            }
        });

        btnAltaTaller.setText("Alta Taller");
        btnAltaTaller.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaTallerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnAltaLocalidad)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtNroTaller)
                                    .addComponent(txtNombre)
                                    .addComponent(txtDireccion)
                                    .addComponent(txtTelefono)
                                    .addComponent(cmbLocalidades, 0, 125, Short.MAX_VALUE))))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(btnAgregarZonaCobertura))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnAltaTaller)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnSalir))
                    .addComponent(jSeparator1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNroTaller, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(cmbLocalidades, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAltaLocalidad)
                    .addComponent(btnAgregarZonaCobertura))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalir)
                    .addComponent(btnAltaTaller))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnAltaTallerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaTallerActionPerformed

        int nroTaller = Integer.valueOf(txtNroTaller.getText());
        String nombre = txtNombre.getText();
        String direccion = txtDireccion.getText();
        String telefono = txtTelefono.getText();
        Localidad unaLocalidad = (Localidad) this.cmbLocalidades.getSelectedItem();
        
        List<Localidad> zonasCobertura = crearListaZonas();
        
        try {
            miVisual.altaTaller(nroTaller, direccion, nombre, telefono, unaLocalidad, zonasCobertura);
        } catch (PreexistingEntityException ex) {
            Logger.getLogger(frmAltaTaller.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(frmAltaTaller.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_btnAltaTallerActionPerformed

    private void btnAltaLocalidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaLocalidadActionPerformed
        frmAltaLocalidad unaLocalidad = new frmAltaLocalidad(miVisual,modeloLocalidades);
        miEscritorio.add(unaLocalidad);
        unaLocalidad.setMaximizable(true);
        unaLocalidad.setClosable(true);
        unaLocalidad.setVisible(true);
    }//GEN-LAST:event_btnAltaLocalidadActionPerformed

    private void btnAgregarZonaCoberturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarZonaCoberturaActionPerformed
        //if(modeloZonas.isEmpty())
        //    modeloZonas.addElement(cmbLocalidades.getSelectedItem());
        Localidades misLocalidades = new Localidades(miEscritorio,miVisual,modeloZonas,listZonasCobertura,modeloLocalidades);
        miEscritorio.add(misLocalidades);
        misLocalidades.setVisible(true);
    }//GEN-LAST:event_btnAgregarZonaCoberturaActionPerformed

    private void cmbLocalidadesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbLocalidadesActionPerformed
       
        
    }//GEN-LAST:event_cmbLocalidadesActionPerformed
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarZonaCobertura;
    private javax.swing.JButton btnAltaLocalidad;
    private javax.swing.JButton btnAltaTaller;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox cmbLocalidades;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JList listZonasCobertura;
    private javax.swing.JTextField txtDireccion;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtNroTaller;
    private javax.swing.JTextField txtTelefono;
    // End of variables declaration//GEN-END:variables

    
    
}
